﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Commons.Cache;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.Security.IServices;
using Yuebon.Security.Models;
using Yuebon.WebApi.Controllers;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Areas.Security.Controllers
{
    /// <summary>
    /// 系统基本信息
    /// </summary>
    [Route("api/Security/[controller]")]
    [ApiController]
    [Authorize]
    public class SysSettingController : BaseController
    {
        /// <summary>
        /// 获取系统基本信息
        /// </summary>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        [HttpGet("Get")]
        public  IActionResult Get(string token)
        {
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            if (result.ErrCode == ErrCode.successCode)
            {
                SysSetting sysSetting = yuebonCacheHelper.Get<SysSetting>("SysSetting");
                if (sysSetting != null)
                {
                    result.ResData = sysSetting;
                    result.Success = true;
                }
                else
                {
                    //初始化系统设置信息
                    sysSetting = XmlConverter.Deserialize<SysSetting>("xmlconfig/sys.config");
                    yuebonCacheHelper.Add("SysSetting", sysSetting);
                    if (sysSetting != null)
                    {
                        result.ResData = sysSetting;
                        result.Success = true;
                    }
                    else
                    {
                        result.ErrMsg = ErrCode.err60001;
                        result.ErrCode = "60001";
                    }
                }
            }
            return ToJsonContent(result);
        }
    }
}